//
//  MainViewController.m
//  DesighBook
//
//  Created by 梅明 on 15/9/11.
//  Copyright (c) 2015年 梅明. All rights reserved.
//

#import "MainViewController.h"


@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBar.hidden = YES;
    _customTabbar.tag = 200;
    [self createTabbar];
}

-(void)createTabbar
{
    NSArray *tabarray =@[@"ico_navbar_caes@3x",@"ico_navbar_pic@3x",@"ico_navbar_qa@3x",@"ico_navbar_find@3x",@"ico_navbar_my@3x"];
    NSArray *tabarray1 = @[@"ico_navbar_caes_on@3x",@"ico_navbar_pic_on@3x",@"ico_navbar_qa_on@3x",@"ico_navbar_find_on@3x",@"ico_navbar_my_on@3x"];
    NSArray *tabarray2 = @[@"案例",@"图库",@"问答",@"发现",@"我的"];
    CGFloat tabwidth = self.view.frame.size.width/5;
    _customTabbar = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-tabwidth/4*3, self.view.frame.size.width, tabwidth/4*3)];
    _customTabbar.userInteractionEnabled = YES;
    self.view.userInteractionEnabled = YES;
    [self.view addSubview:_customTabbar];
    
    for (int i = 0; i<5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(tabwidth*i, 0, tabwidth, tabwidth/4*3);
        
        [button setImage:[UIImage imageNamed:tabarray[i]] forState:UIControlStateNormal];
        [button setTitle:tabarray2[i] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor whiteColor];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:10]];
        
        //点击后的button
        [button setImage:[UIImage imageNamed:tabarray1[i]] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        //button图片和title的偏移
        //UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
        button.titleEdgeInsets = UIEdgeInsetsMake(25, 0, 0, 20);
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 20, 0);
        
        //button添加标签
        button.tag = 100+i;
        if (i==0) {
            button.selected = YES;
        }
        
        //button点击事件
        [button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
        [_customTabbar addSubview:button];
    }
}


-(void)tap:(UIButton *)button
{
    for (int i = 0; i<5; i++) {
        UIButton *b = (UIButton *)[_customTabbar viewWithTag:100+i];
        b.selected = NO;
    }
    button.selected = YES;
    
    self.selectedIndex = button.tag-100;
    
}

-(void)HiddenTabBar:(BOOL)boll
{
    _customTabbar.hidden = YES;
}




@end
